﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using TetrisClient.TetrisService;

namespace TetrisClient
{
    /// <summary>
    /// Class responsible for handling the events generated on the Login Page.
    /// </summary>
    public partial class LoginPage : UserControl
    {
        private TetrisWebServiceSoapClient proxy;
        private User user;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoginPage"/> class.
        /// </summary>
        public LoginPage()
        {
            InitializeComponent();
            proxy = new TetrisWebServiceSoapClient();

            user = new User() { };

            globalLoginStackPanel.DataContext = user;
            globalSignUpStackPanel.DataContext = user;

            proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
            proxy.SignUpCompleted += new EventHandler<SignUpCompletedEventArgs>(proxy_SignUpCompleted);
        }

        /// <summary>
        /// Handles the SignUpCompleted event of the proxy control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="SignUpCompletedEventArgs"/> instance containing the event data.</param>
        private void proxy_SignUpCompleted(Object sender, SignUpCompletedEventArgs args)
        {
            if (args.Error == null)
            {
                if ((bool)args.Result)
                {
                    globalSignUpStackPanel.Visibility = System.Windows.Visibility.Collapsed;
                    globalLoginStackPanel.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    loginSignUpErrorLabel.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        /// <summary>
        /// Handles the LoginCompleted event of the proxy control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="LoginCompletedEventArgs"/> instance containing the event data.</param>
        private void proxy_LoginCompleted(Object sender, LoginCompletedEventArgs args)
        {
            if (args.Error == null)
            {
                if ((bool) args.Result)
                {
                    loginErrorLabel.Visibility = System.Windows.Visibility.Collapsed;

                    proxy.CloseAsync();

                    this.Content = new TetrisPage(user.Login);
                }
                else
                {
                    loginErrorLabel.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        /// <summary>
        /// Handles the backSignUpPage event of the button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        void button_backSignUpPage(object sender, RoutedEventArgs e)
        {
            globalSignUpStackPanel.Visibility = System.Windows.Visibility.Collapsed;
            globalLoginStackPanel.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// Handles the signUpPage event of the button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        void button_signUpPage(object sender, RoutedEventArgs e)
        {
            globalSignUpStackPanel.Visibility = System.Windows.Visibility.Visible;
            globalLoginStackPanel.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// Handles the login event of the button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        void button_login(object sender, RoutedEventArgs e)
        {
            loginTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            passwordTextBox.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();

            if (user.Login != null && user.Password != null)
            {
                proxy.LoginAsync(user.Login, user.Password);
            }
        }

        /// <summary>
        /// Handles the signUp event of the button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        void button_signUp(object sender, RoutedEventArgs e)
        {
            loginSignUpTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            passwordSignUpTextBox.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();
            confirmPasswordSignUpTextBox.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();

            if (user.Login != null && user.Password != null &&
                user.ConfirmPassword != null && user.Password == user.ConfirmPassword)
            {
                proxy.SignUpAsync(user.Login, user.Password);
            }
        }

    }
}
